package com.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.user.mapper.PositionMapper;
import com.user.pojo.Position;
import com.user.pojo.User;
import com.user.pojo.data;
import com.user.service.UserService;
import com.user.mapper.UserMapper;
import com.user.reuslt.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @description 针对表【user(用户表)】的数据库操作Service实现
 * @createDate 2024-09-20 10:20:16
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService
{

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PositionMapper positionMapper;


    @Override
    public Result DataUserService()
    {
        List<User> userss = userMapper.selectList(null);
        return Result.data(userss);
    }

    @Override
    public Result DataPositionService()
    {
        List<Position> Positions = positionMapper.selectList(null);
        return Result.data(Positions);
    }

    public List<data> getUserData()
    {
        return userMapper.selectUserData();
    }

    @Override
    public Result Data()
    {
        List<data> data = getUserData();
        return Result.data(data);
    }

    @Override
    public Result QueryService(data data)
    {
        com.user.pojo.data data_d = data;
        System.out.println(data_d);

        LambdaQueryWrapper<User> userWrapper = new LambdaQueryWrapper<User>().like(User::getUsername, data.getUsername());
        List<User> data1 = userMapper.selectList(userWrapper);

        LambdaQueryWrapper<Position> positionWrapper = new LambdaQueryWrapper<Position>().eq(Position::getId, data1.get(0).getPositionId());
        List<Position> data2 = positionMapper.selectList(positionWrapper);

        List<Object> Data0 = new ArrayList<>();
        Data0.addAll(data1);
        Data0.addAll(data2);
        return Result.data(Data0);
    }


    @Override
    public Result InsertUserService(User User)
    {
        userMapper.insert(User);
        return Result.data("OK");
    }

    @Override
    public Result DeleteService(data data)
    {
        LambdaQueryWrapper<User> userWrapper = new LambdaQueryWrapper<User>().eq(User::getUsername, data.getUsername());
        List<User> data1 = userMapper.selectList(userWrapper);
        userMapper.deleteBatchIds(data1);
        return Result.data("OK");
    }

    @Override
    public Result UpdateService(data data)
    {
        LambdaQueryWrapper<User> userWrapper = new LambdaQueryWrapper<User>().eq(User::getUsername, data.getUsername());
        User user = userMapper.selectOne(userWrapper);
        System.out.println("我是selectOne: " + user);
        user.setUsername(data.getUsername());
        user.setPassword(data.getPassword());
        user.setCreatedTime(data.getCreatedTime());
        user.setCreatedBy(data.getCreatedBy());
        user.setPositionId(data.getPositionId());
        userMapper.updateById(user);

        LambdaQueryWrapper<Position> positionWrapper = new LambdaQueryWrapper<Position>().eq(Position::getId, user.getPositionId());
        Position position = positionMapper.selectOne(positionWrapper);

        data data_d = data;
        System.out.println("我是post: " + data_d);
        System.out.println("我是结果: " + user + position + "\n");
        return Result.data("OK");
    }

    @Override
    public Result batchUpData(List<data> data)
    {
        for (data d : data)
        {
            UpdateService(d);
        }

        return Result.data("OK");
    }


}



